//
// Created by Symps on 2025/9/20.
//

#include <iostream>
#include <regex>
using std::endl;
using std::cout;
#include <stack>
using std::stack;
using std::string;

void test1() {
    const string s("Hello World!");
    for (const auto c : s) {
        cout << c << endl;
    }
}

void test2() {
    // string::iterator i;
    // for (const string s("Hello World!"); i != s.end(); ++i) {
    //     cout << *i << endl;
    // }
    int arr[10] = {
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    };
    for (const int val : arr) {
        cout << val << endl;
    }
}

void test3() {
    string s1("Hello你好");
    cout << s1.length() << endl;
    cout << s1.size() << endl;
    cout << s1.capacity() << endl;
    s1 = "aaa";
    cout << s1 << endl;
}

void test4() {
    // stack<int> s1;
    // for (int i = 0; i < 100; ++i) {
    //     s1.push(i + 1);
    // }
    // s1.

    string s("Hello World!");
    cout << s.capacity() << endl;
    s.reserve(20);
    cout << s.capacity() << endl;
    s.shrink_to_fit();
    cout << s.capacity() << endl;
    // s.resize(5);
    // cout << s << endl;
    // s.resize(30);
    // cout << s << endl;
    const string s2 = "AAA建材批发王总";
    s.append(s2);
    // s += "AAA建材批发王总";
    // s.append(++s2.begin(), s2.end());
    cout << s << endl;
}

void test5() {
    string s1("aaa");
    cout << (s1 += "1") << endl;
    cout << s1 + "1" << endl;
    cout << s1 << endl;
    s1.assign("abcdefg");
    cout << s1 << endl;
    auto s2 = s1.substr(1, 4);
    cout << s2 << endl;
    s2.insert(1, "123");
    cout << s2 << endl;
    s2.erase(1, 4);
    cout << s2 << endl;
    s2.replace(0, 2, "123");
    cout << s2 << endl;
    cout << s2.find("3") << endl;
}

void test6() {
    string s1;
    for (int i = 0; i < 10000; ++i) {
        s1 += "abc    def ghi";
    }

    clock_t c1 = clock();
    while (s1.find(" ") != string::npos) {
        s1.replace(s1.find(" "), 1, "%");
    }
    c1 = clock() - c1;

    s1 = "";
    for (int i = 0; i < 10000; ++i) {
        s1 += "abc    def ghi";
    }
    string s2;
    clock_t c2 = clock();
    for (const auto c : s1) {
        if (c == ' ') {
            s2 += "%";
        }
        else {
            s2 += c;
        }
    }
    c2 = clock() - c2;

    cout << "c1: " << c1 << endl;
    cout << "c2: " << c2 << endl;
}

void test7() {
    std::string str = "Hello, my email is example@example.com and my phone number is 123-456-7890";

    // 创建一个正则表达式对象，匹配邮箱地址
    std::regex email_regex(R"(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b)");

    // 在字符串中查找所有匹配的邮箱地址
    std::smatch matches;
    std::string::const_iterator searchStart(str.cbegin());
    while (std::regex_search(searchStart, str.cend(), matches, email_regex)) {
        std::cout << "Found email: " << matches[0] << std::endl;
        searchStart = matches.suffix().first;
    }

    // 创建一个正则表达式对象，匹配电话号码
    std::regex phone_regex(R"(\d{3}-\d{3}-\d{4})");

    // 在字符串中查找所有匹配的电话号码
    searchStart = str.cbegin();
    while (std::regex_search(searchStart, str.cend(), matches, phone_regex)) {
        std::cout << "Found phone number: " << matches[0] << std::endl;
        searchStart = matches.suffix().first;
    }

    // 替换字符串中的匹配项
    std::string result = std::regex_replace(str, email_regex, "REDACTED");
    std::cout << "Replaced string: " << result << std::endl;
}

void test8() {
    string str = "AAAAA T";
    int x = str.length() - str.rfind(' ') - 1;
    cout << x << endl;
    std::getline(std::cin, str, 'b');
    cout << str << endl;
}

int firstUniqChar(string s) {
    int count[26] = {};
    for (const char c : s) {
        count[c - 'a']++;
    }
    int j = 0;
    for (const int c : count) {
        cout << (char)(j + 'a') << c << " ";
        j++;
    }
    cout << endl;

    for (int i = 0; i < s.length(); ++i) {
        if (count[s[i] - 'a'] == 1) {
            return i;
        }
    }
    return -1;
}

void test9() {
    cout << firstUniqChar("loveleetcode") << endl;
}

string addStrings(string num1, string num2) {
    string ret = "";
    int least1 = num1.size() - 1, least2 = num2.size() - 1;
    int C = 0;
    while (least1 >= 0 || least2 >= 0) {
        int v1 = least1 >= 0 ? num1[least1--] - '0' : 0;
        int v2 = least2 >= 0 ? num2[least2--] - '0' : 0;
        int S = v1 + v2 + C;
        C = S / 10;
        S %= 10;
        ret += S + '0';
    }

    if (C != 0) {
        ret += '1';
    }
    std::reverse(ret.begin(), ret.end());
    return ret;
}

string multiply(string num1, string num2) {
    if (num1 == "0" || num2 == "0") {
        return "0";
    }
    int end1 = num1.length() - 1;
    int end2 = num2.length() - 1;

    string ret = "0";

    for (int i = end2; i >= 0; --i) {
        int C = 0;
        string retOfOne = "";
        for (int j = end1; j >= 0; --j) {
            int P = (num1[j] - '0') * (num2[i] - '0') + C;
            C = P / 10;
            P %= 10;
            retOfOne += P + '0';
        }
        if (C) {
            retOfOne += C + '0';
        }
        std::reverse(retOfOne.begin(), retOfOne.end());
        int x = end2 - i;
        while (x--) {
            retOfOne += '0';
        }
        ret = addStrings(ret, retOfOne);
    }

    return ret;
}

void test10(string (*f)(string, string)) {
    string n1[] = {
        "21",
        "8",
        "123",
        "99"
    };
    string n2[] = {
        "21",
        "678",
        "456",
        "444"
    };

    constexpr int n = std::size(n1);
    for (int i = 0; i < n; ++i) {
        cout << multiply(n1[i], n2[i]) << endl;
    }
}

string multiply2(string num1, string num2) {
    if (num1 == "0" || num2 == "0") {
        return "0";
    }

    int size1 = num1.size(), size2 = num2.size();
    std::vector retInt(size1 + size2, 0);

    for (int i = size1 - 1; i >= 0; --i) {
        for (int j = size2 - 1; j >= 0; --j) {
            int P = (num1[i] - '0') * (num2[j] - '0');
            int index = i + j + 1;
            int S = P + retInt[index];
            retInt[index] = S % 10;
            retInt[index - 1] += S / 10;
        }
    }

    string ret;
    for (const int num : retInt) {
        if (!(ret.empty() && num == 0)) {
            ret += num + '0';
        }
    }
    return ret;
}

void reverse(string& s, size_t begin, size_t end) {
    for (; begin < end; ++begin, --end) {
        const char c = s[begin];
        s[begin] = s[end];
        s[end] = c;
    }
}

string reverseWords(string s) {
    // 快速去除前导空格
    int i = 0;
    while (s[i] == ' ') {
        ++i;
    }
    int left = 0;
    const size_t end = s.length();
    size_t right = end - 1;
    for (; i < end; ++i) {
        if (s[i] != ' ') {
            if (i != 0 && s[i - 1] == ' ') {
                left = i;
            }
            if (i != end - 1 && s[i + 1] == ' ') {
                right = i;
            }
            if (i == end - 1) {
                reverse(s, left, end - 1);
            }
        }
        else {
            reverse(s, left, right);
        }
    }
    return s;
}

void test11() {
    cout << reverseWords("Let's take LeetCode contest") << endl;
}

void test12() {
    int max = std::max(1, 2);
    cout << max << endl;
}

int maxArea(std::vector<int>& height) {
    int right = height.size() - 1;
    int left = 0;
    int max = 0;
    while (left < right) {
        max = std::max(max, std::min(height[left], height[right]) * (right - left));
        if (height[left] <= height[right]) {
            ++left;
        }
        else if (height[left] > height[right]) {
            --right;
        }
    }
    return max;
}

void test13() {
    std::vector height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
    cout << maxArea(height) << endl;
}

void parseURL(const string& url) {
    int i1 = url.find(':');
    string protocol = url.substr(0, i1);
    i1 += 3;
    int i2 = url.find('/', i1);
    string host = url.substr(i1, i2 - i1);
    i2 += 1;
    string path = url.substr(i2);
    cout << protocol << endl;
    cout << host << endl;
    cout << path << endl;
    // }
}

void test14() {
    parseURL("https://leetcode.cn/problems/two-sum/description/");
}

int main() {
    // test1();
    // test2();
    // test3();
    // test4();
    // test5();
    // test6();
    // test7();
    // test8();
    // test9();
    // test10(multiply);
    // test10(multiply2);
    // test11();
    // test12();
    // test13();
    test14();
    return 0;
}
